package com.example.presentshopping.ui.fragment.PromoteOrder

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.StartersBean
import com.example.presentshopping.exent.UserEvent
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import org.greenrobot.eventbus.EventBus

class StarterstowFragment(var Str: String) : BaseMvpFragment() {


    var starasBean: StartersBean? = null


    var baseRecyclerAdapter: BaseRecyclerAdapter<StartersBean.DataBean.ListBean>? = null
    var recycler_view: SwipeRecyclerView? = null

    var page = 1

    private var queryInfoPresenter: PublicPresenter? = null
    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)

        recycler_view = view?.findViewById(R.id.recycler_view)
        setAdapter()
        OnseVIen()
        // 使用默认的加载更多的View。
        recycler_view?.useDefaultLoadMore()
        recycler_view?.loadMoreFinish(false, true)
        recycler_view?.setLoadMoreListener {
            page++
            OnseVIen()
            Log.e("StartersFragment",page.toString())
        }


    }

    fun OnseVIen() {
        queryInfoPresenter?.netWorkRequestPost(
            HttpURL.Req_Myteam, HttpRequest.getMyteam(
                Str,
                "",
                "",
                "",
                page.toString(),
                "10"
            )
        )

    }

    fun setAdapter() {
        baseRecyclerAdapter = object : BaseRecyclerAdapter<StartersBean.DataBean.ListBean>(
            context,
            null,
            R.layout.iemt_starters
        ) {
            @SuppressLint("SetTextI18n")
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: StartersBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val mobile = holder?.getView<TextView>(R.id.mobile)
                val create_time = holder?.getView<TextView>(R.id.create_time)
                val order_num = holder?.getView<TextView>(R.id.order_num)
                val total_money = holder?.getView<TextView>(R.id.total_money)
                val count_num = holder?.getView<TextView>(R.id.count_num)

                val yesterday_total_money = holder?.getView<TextView>(R.id.yesterday_total_money)
                val yesterday_order_num = holder?.getView<TextView>(R.id.yesterday_order_num)

                val name =data.nickname

                create_time?.text = "加入时间：" + data.create_time
                order_num?.text = "订单总量：" + data.order_num
                total_money?.text = "红包总额：" + data.total_money
                mobile?.text = "用户：($name) " +data.mobile
                yesterday_total_money?.text = "昨日订单: "+ data.yesterday_order_num
                yesterday_order_num?.text ="昨日红包: " +data.yesterday_total_money.toString()

                if (Str=="2"){
                    count_num?.visibility = View.GONE
                }else{
                    count_num?.text ="推广人数 ："+data.count_num
                }

            }
        }

        recycler_view?.adapter = baseRecyclerAdapter


    }

    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        super.onDestroy()
    }

    override fun onSuccess(data: String?) {

        starasBean = GsonUtils.getGson().fromJson(data, StartersBean().javaClass)
        recycler_view?.loadMoreFinish(false, true)
        Log.e("StartersFragment",""+starasBean?.data?.list?.size)
        Log.e("StartersFragment",data.toString())
        if(starasBean?.data?.list?.size!!>0){

            baseRecyclerAdapter?.setData(starasBean?.data?.list)
        }else{
            recycler_view?.loadMoreFinish(true, false)
        }


    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

        Log.e("StartersFragment","onFailure")
    }



    override val layoutId: Int
        get() = R.layout.starters_fragment
}